package com.jyh.dbUser.dao;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONObject;
import com.jyh.config.BaseAction;
import com.jyh.util.Time;

@Repository
public class PersonInfoDao extends BaseAction{
	@Autowired
	private JdbcTemplate jdbcTemplate;
	/**
	 * 获取用户个人信息
	 * @return
	 */

	public List<Map<String, Object>> getlist(String sql) {
		List<Map<String, Object>> list = jdbcTemplate.query(sql, new RowMapper<Map<String, Object>>() {
			
			public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
				ResultSetMetaData m = rs.getMetaData();
				int columns = m.getColumnCount();
				Map<String, Object> map = new HashMap<>();
				// 显示列,表格的表头
				for (int i = 1; i <= columns; i++) {
					Object val = rs.getObject(m.getColumnLabel(i));
					if(val != null && val instanceof Date){
						val = Time.DATE_TIME_FORMAT.format(val);
					}
					if(val == null)
						map.put(m.getColumnLabel(i),  null);
					else
						map.put(m.getColumnLabel(i),  val.toString());
				}
				return map;
			}
		});
		logger.info("获取页面数据集合list={}",JSONObject.toJSONString(list));
		return list;
	}
	
	
	/**
	 * 获取数据
	 * @return
	 */
	
	public List<Map<String, Object>> getListMap(String sql) {
		logger.info("获取页面数据SQL={}",sql);
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		
		try {
			list = jdbcTemplate.queryForList(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		logger.info("获取页面数据集合list={}",JSONObject.toJSONString(list));
		return list;
	}
	
	/**
	 * 获取数据
	 * @return
	 */
	
	public Map<String, Object> getMap(String sql) {
		logger.info("获取页面数据SQL={}",sql);
		Map<String,Object> map = new HashMap<String,Object>();
		
		try {
			map = jdbcTemplate.queryForMap(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		logger.info("获取页面数据集合list={}",JSONObject.toJSONString(map));
		return map;
	}
}
